package com.dongdongshop.controller;
import com.aliyuncs.exceptions.ClientException;
import com.dongdongshop.util.SmsUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


@Service
@RocketMQMessageListener(topic = "CCTV8",consumerGroup = "myMq")
public class SMSController implements RocketMQListener<String> {

    @Resource
    private SmsUtil smsUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    //生成手机号，存入redis中
    @Override
    public void onMessage(String s) {
        String s1 = null;
        try {
            s1 = smsUtil.sendSms(s);
            String key = "phoneNumber";
            String value = s1;
            redisTemplate.boundHashOps(key).put(s,value);
            redisTemplate.boundHashOps(key).expire(5, TimeUnit.MINUTES);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
